<template>
  <div>
    <h2 class="detail-title">实验基本信息</h2>
    <div class="detail-info">
      <div class="detail-item">实验ID：{{ data.id }}</div>
      <div class="detail-item">实验名称：{{ data.experiment_name }}</div>
      <div class="detail-item">实验时长：{{ data.duration }}分钟</div>
<!--      <div class="detail-item">截止时间：{{ data.ending_time }}</div>-->
<!--      <div class="detail-item">知识点列表：{{ data.knowledges }}</div>-->
<!--      <div class="detail-item">关联知识点路径：{{ data.konwledges_path }}</div>-->
    </div>

    <h2 class="detail-title">实验环境基本信息</h2>
    <div class="detail-info">
      <div class="detail-item">实验环境ID：{{ env.id }}</div>
      <div class="detail-item">实验环境名称：{{ data.experiment_name }}</div>
      <div class="detail-item">镜像名称：{{ env.images }}</div>
<!--      <div class="detail-item">子网名称：{{ env.subnet_name }}</div>-->
<!--      <div class="detail-item">子网地址：{{ env.cidr_block }}</div>-->
      <div class="detail-item">安全组：{{ security_group }}</div>
      <div class="detail-item">子网地址：{{ cidr_block }}</div>
      <div class="detail-item">是否外接：{{is_open}}</div>
      <div class="detail-item">创建时间：{{ env.create_time }}</div>
    </div>
    <h2 class="detail-title">实例列表</h2>
    <TableBar>
      <el-button @click="onSend(env)" size="success" class="el-icon-plus">批量创建</el-button>
      <el-button @click="onMulRec" type="primary" icon="el-icon-delete">批量回收</el-button>
      <el-button @click="onMulSd" type="success" icon="el-icon-switch-button">批量关机</el-button>
      <el-button @click="onMulStartUp" type="primary" icon="el-icon-switch-button">批量开机</el-button>
    </TableBar>
    <div class="detail-info">
      <el-table :data="tableData" @selection-change="onSelectionChange" style="width: 100%">
        <el-table-column fixed="left" type="selection" width="55"></el-table-column>
        <el-table-column fixed="left" type="index" label="序号" width="55"></el-table-column>
        <el-table-column prop="id" label="实验环境id"></el-table-column>
        <el-table-column prop="instanceId" label="实例ID"></el-table-column>
        <el-table-column prop="instance_name" label="实例名称"></el-table-column>
        <el-table-column prop="images" label="镜像名称"></el-table-column>
<!--        <el-table-column prop="ip_address" label="ip地址"></el-table-column>-->
        <el-table-column prop="status" label="实例状态"></el-table-column>
<!--        <el-table-column prop="studentid" label="学号"></el-table-column>-->
        <el-table-column prop="username" label="用户姓名"></el-table-column>
        <!--        <el-table-column label="操作">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-button-->
        <!--              type="text"-->
        <!--              @click="onRecall(scope.row)"-->
        <!--              size="small"-->
        <!--              class="text-button"-->
        <!--            >回收</el-button>-->
        <!--             <el-button @click="onShutdown(scope.row)" size="small" class="text-error">关机</el-button>-->
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
                type="primary"
                @click="onRecall(scope.row)"
                size="mini"
                class="text-button"
                icon="el-icon-delete"
            >回收
            </el-button>
            <el-button @click="onShutdown(scope.row)" v-if="scope.row.status='RUNNING'" size="mini" type="success" icon="el-icon-switch-button">关机</el-button>
            <el-button @click="onStartup(scope.row)" v-else size="mini" type="primary" icon="el-icon-switch-button">开机</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      background
      layout="prev, pager, next, sizes, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-sizes="page_sizes"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>
    <InstCreateBatch :visible="dialogVisible" @onClose="onClose" :env_id="env_id"/>
    <el-button class="go-back-btn" @click="goBack" type="primary">返回</el-button>
  </div>
</template>
<script>
import goBack from "@/mixins/goBack";
import pageMixin from '@/mixins/pageMixin.js'
import { experimentModel } from "../api/models";
import selects from '@/common/selects.js';
import {Message} from 'element-ui';
import TableBar from '@/components/TableBar'
import InstCreateBatch from '@/mixins/instCreateBatch.js';
import openStatus from "@/common/is_open_status";
export default {
  mixins: [goBack, pageMixin,InstCreateBatch],
  components: {TableBar},
  data() {
    return {
      data: {},
      id: null,
      env: {},
      total: 0,
      tableData: [],
      search: {
        page: 1,
        page_size: 10
      },
      mulSelectedIns: [],
      cidr_block: selects.subnets[0].label,
      is_open: openStatus.status[0].label,
      security_group: selects.security_group[1].label
    };
  },
  mounted() {
    this.id = this.$route.params.id;
    this.getItem();
  },
  methods: {
    onSelectionChange(val) {
      this.mulSelectedIns = val;
    },
    onMulStartUp() {
      if(this.mulSelectedIns.length === 0) {
        return Message.warning('请先选择需要处理实例');
      }
      let ins = this.mulSelectedIns.map(v => {
        return v.id;
      })
      this.doStartUp(ins);
    },
    onMulSd() {
      if(this.mulSelectedIns.length === 0) {
        return Message.warning('请先选择需要处理实例');
      }
      let ins = this.mulSelectedIns.map(v => {
        return v.id;
      })
      this.doShutdown(ins);
    },
    onMulRec() {
      if(this.mulSelectedIns.length === 0) {
        return Message.warning('请先选择需要处理实例');
      }
      let ins = this.mulSelectedIns.map(v => {
        return v.id;
      })
      this.doRecall(ins);
    },
    doRecall(instances) {
      this.$confirm('确定回收选中实例？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        experimentModel.insBatchRec({
          instances: instances
        }, res => {
          this.$message({
            type: 'success',
            message: res.message
          });
          this.getEnvs();
        }, err => {
          console.log(err)
        })
      }).catch((err) => {
        console.error(err)
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    doShutdown(instances) {
      this.$confirm('确定关机选中实例？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        experimentModel.insBatchSd({
          instances: instances
        }, res => {
          this.$message({
            type: 'success',
            message: res.message
          });
          this.getEnvs();
        }, err => {
          console.log(err)
        })
      }).catch((err) => {
        console.error(err)
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    doStartUp(instances) {
      this.$confirm('确定开机选中实例？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        experimentModel.insBatchStartUp({
          instances: instances
        }, res => {
          this.$message({
            type: 'success',
            message: res.message
          });
          this.getEnvs();
        }, err => {
          console.log(err)
        })
      }).catch((err) => {
        console.error(err)
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    onRecall(row) {
      this.doRecall([row.id])
    },
    onShutdown(row) {
      this.doShutdown([row.id])
    },
    onStartup(row) {
      this.doStartUp([row.id])
    },
    getEnvs() {
      console.log("获取实验相关实例")
      experimentModel.envIns(this.id, res => {
        this.tableData = res.data
        for (let ele of this.tableData) {
          ele.images = this.env.image
        }
        console.log(this.tableData)
      })
    },
    getItem() {

      experimentModel.env(this.id, res => {
        // console.log(res.data[0])
        this.env = res.data[0];
        this.data = res.data[1];
        // if(this.env.experiment_id) {
        if (this.data.id) {
          this.getEnvs()
          // experimentModel.item(this.env.experiment_id, res => {
          //   this.data = res.data
          // })
        }
      });
    }
  }
};
</script>
<style lang="scss">
</style>